﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;//
using Tank.Properties;//

namespace Tank
{
    /// <summary>
    /// 敌方坦克
    /// </summary>
    public class Enemy : Roles
    {
        #region 敌方坦克造型图

        /// <summary>
        /// 敌方坦克No.001
        /// </summary>
        private static Image[] eImg_001 = { Resources.E1U, Resources.E1D, Resources.E1L, Resources.E1R };

        /// <summary>
        /// 敌方坦克No.002
        /// </summary>
        private static Image[] eImg_002 = { Resources.E2U, Resources.E2D, Resources.E2L, Resources.E2R };

        /// <summary>
        /// 敌方坦克No.003
        /// </summary>
        private static Image[] eImg_003 = { Resources.E3U, Resources.E3D, Resources.E3L, Resources.E3R };

        /// <summary>
        /// 敌方坦克速度
        /// </summary>
        private static int eSp;

        /// <summary>
        /// 敌方坦克生命
        /// </summary>
        private static int eLife;

        /// <summary>
        /// 敌方坦克攻击力
        /// </summary>
        private static int eAp;

        /// <summary>
        /// 敌方坦克防御力
        /// </summary>
        private static int eDp;

        #endregion

        #region 敌方坦克构造函数

        public Enemy()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="x">敌人的x座标</param>
        /// <param name="y">敌人的y座标</param>
        /// <param name="life">敌人的生命</param>
        /// <param name="speed">敌人的速度</param>
        /// <param name="dir">敌人的方向</param>
        public Enemy(int x, int y, int type, Direction dir)
            : base(x, y, eImg_001[0].Width, eImg_001[0].Height, SetELife(type), SetESpeed(type), dir)
        {
            this.EType = type;
            Rness = new Random();
            this.Enable = true;
            this.Ap = SetAp(type);
            this.Dp = SetDp(type);
        }

        #endregion

        #region 敌方坦克各项方法

        #region 敌方坦克设置

        /// <summary>
        /// 设置敌方坦克速度
        /// </summary>
        /// <returns></returns>
        private static int SetESpeed(int eType)
        {
            switch (eType)
            {
                case 0:
                    eSp = 1;
                    break;
                case 1:
                    eSp = 3;
                    break;
                case 2:
                    eSp = 2;
                    break;
            }

            return eSp;
        }

        /// <summary>
        /// 设置敌方坦克生命
        /// </summary>
        /// <returns></returns>
        private static int SetELife(int eType)
        {
            switch (eType)
            {
                case 0:
                    eLife = 1;
                    break;
                case 1:
                    eLife = 1;
                    break;
                case 2:
                    eLife = 1;
                    break;
            }
            return eLife;
        }

        /// <summary>
        /// 设置敌方坦克攻击力
        /// </summary>
        /// <param name="eType"></param>
        /// <returns></returns>
        private static int SetAp(int eType)
        {
            switch (eType)
            {
                case 0:
                    eAp = 10;
                    break;
                case 1:
                    eAp = 20;
                    break;
                case 2:
                    eAp = 30;
                    break;
            }
            return eAp;
        }


        /// <summary>
        /// 设置敌方坦克防御力
        /// </summary>
        /// <param name="eType"></param>
        /// <returns></returns>
        private static int SetDp(int eType)
        {
            switch (eType)
            {
                case 0:
                    eDp = 10;
                    break;
                case 1:
                    eDp = 15;
                    break;
                case 2:
                    eDp = 20;
                    break;
            }
            return eDp;
        }
        #endregion

        #region 敌方坦克战斗、移动

        /// <summary>
        /// 敌方坦克移动
        /// </summary>
        public override void Move()
        {
            if (Enable)
            {
                AdjustDir();
                base.Move();
                if (Rness.Next(100) < 2)//这几率不能太BT了啊
                {
                    Fire();
                } 
            }
            IsDoom();//是否被摧毁
        }

        /// <summary>
        /// 实现判断是否被击毁（enemy）
        /// </summary>
        public override void IsDoom()
        {
            if (this.Life <= 0)
            {
                Sound.Play(@"Sound\blast.wav");
                GameMana.Instance().AddGameElement(new Blast(this.X, this.Y));
                GameMana.Instance().DelGameElement(this);
            }
        }

        /// <summary>
        /// 绘制敌方坦克移动战斗
        /// </summary>
        /// <param name="g"></param>
        public override void Draw(Graphics g)
        {
            Move();
            switch (EType)
            {
                case 1:
                    switch (Dir)
                    {
                        case Direction.U:
                            g.DrawImage(eImg_001[0], this.X, this.Y);
                            break;
                        case Direction.D:
                            g.DrawImage(eImg_001[1], this.X, this.Y);
                            break;
                        case Direction.L:
                            g.DrawImage(eImg_001[2], this.X, this.Y);
                            break;
                        case Direction.R:
                            g.DrawImage(eImg_001[3], this.X, this.Y);
                            break;
                    }
                    break;

                case 2:
                    switch (Dir)
                    {
                        case Direction.U:
                            g.DrawImage(eImg_002[0], this.X, this.Y);
                            break;
                        case Direction.D:
                            g.DrawImage(eImg_002[1], this.X, this.Y);
                            break;
                        case Direction.L:
                            g.DrawImage(eImg_002[2], this.X, this.Y);
                            break;
                        case Direction.R:
                            g.DrawImage(eImg_002[3], this.X, this.Y);
                            break;
                    }
                    break;

                case 3:
                    switch (Dir)
                    {
                        case Direction.U:
                            g.DrawImage(eImg_003[0], this.X, this.Y);
                            break;
                        case Direction.D:
                            g.DrawImage(eImg_003[1], this.X, this.Y);
                            break;
                        case Direction.L:
                            g.DrawImage(eImg_003[2], this.X, this.Y);
                            break;
                        case Direction.R:
                            g.DrawImage(eImg_003[3], this.X, this.Y);
                            break;
                    }
                    break;
            }
            base.ShowHP(g);
        }

        /// <summary>
        /// 调整移动方向(Enemy)
        /// </summary>
        public override void AdjustDir()
        {
            if ((this.X==0 && this.Dir == Direction.L) || (this.X==720 && this.Dir == Direction.R)
                ||
                (this.Y==0 && this.Dir == Direction.U) || (this.Y==540 && this.Dir == Direction.D)
                ||
                this.IsSuffocate)
            {
                this.IsSuffocate = false;
                switch (Rness.Next(4))
                {
                    case 0:
                        this.Dir = Direction.U;
                        break;
                    case 1:
                        this.Dir = Direction.D;
                        break;
                    case 2:
                        this.Dir = Direction.L;
                        break;
                    case 3:
                        this.Dir = Direction.R;
                        break;
                }
            }            
        }

        /// <summary>
        /// 射击(Enemy)
        /// </summary>
        public override void Fire()
        {
          //  GameMana.Instance().AddGameElement(new EShell(this, 1, 10, 1));
        }
        #endregion
        #endregion


        #region 敌方坦克属性

        /// <summary>
        /// 敌方坦克类型
        /// </summary>
        public int EType { get; set; }

        /// <summary>
        /// 人品器
        /// </summary>
        public Random Rness { get; set; }

        /// <summary>
        /// 行动是否受阻
        /// </summary>
        public bool IsSuffocate { get; set; }

        /// <summary>
        /// 受阻方向（1上、2下、3左、4右）
        /// </summary>
        //public int SuffDir { get; set; }
        #endregion

    }
}
